package org.xinjiang.data.migration.common.utils.query;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.CaseFormat;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * 查询帮助类
 *
 * @author linkai
 * @date 2022/11/25
 */
public interface Queries {

    static String likeVal(String s) {
        return "%" + s + "%";
    }

    static String lowerUnderscore(String s) {
        return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, s);
    }

    static <T> QueryWrapper<T> wrapper() {
        return new QueryWrapper<>();
    }

    static <T> QueryWrapper<T> wrapper(T t) {
        return new QueryWrapper<>(t);
    }

    static <T> LambdaQueryWrapper<T> lambda() {
        return Queries.<T>wrapper().lambda();
    }

    static <T> LambdaQueryWrapper<T> lambda(T t) {
        return Queries.wrapper(t).lambda();
    }

    static <T> QueryWrapper<T> eq(String column, Object val) {
        return Queries.<T>wrapper().eq(column, val);
    }

    static <T> QueryWrapper<T> in(String column, List<?> val) {
        return Queries.<T>wrapper().in(column, val);
    }

    static void like(String s, Consumer<String> c) {
        if (StringUtils.isNotEmpty(s)) {
            c.accept(joint(s));
        }
    }

    static void accept(String s, Consumer<String> c) {
        if (StringUtils.isNotEmpty(s)) {
            c.accept(s);
        }
    }

    static <T> void accept(T s, Consumer<T> c) {
        if (Objects.nonNull(s)) {
            c.accept(s);
        }
    }

    static String joint(String s) {
        return "%" + s + "%";
    }

    static <V, R> R apply(V v, Function<? super V, R> action) {
        if (v == null) {
            return null;
        }
        if (v instanceof String) {
            if ("".equals(((String) v).trim())) {
                return null;
            }
        }
        return action.apply(v);
    }

}
